// pages/search/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden:true,
    List:[],
    value:'',
    historyList:[]
  },
  timeId:999,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      historyList:wx.getStorageSync('historyList')
    }
      )
  },
  clear(){
    this.setData({
      List:[],
      hidden:true,
      value:''
    })
  },
  handleC(){
    this.setData({
      historyList:[]
    })
    wx.setStorageSync('historyList', [])
  },
  handleInput(e){
    let {value} = e.detail
    if(!value.trim()){
      this.setData({
        value:'',
        hidden:true,
        List:[]
      })
      return
    }
    this.setData({
      hidden:false,
    })
    clearTimeout(this.timeId)
    var that =this
    this.timeId =setTimeout(function(){
      that.setList(value)
    },1000)
  },
  TurnTo(e) {
    var products = e.currentTarget.dataset.products
    var pro = JSON.stringify(products)
    wx.navigateTo({
        url: products.url + '?products=' + pro,
    })
},
setvalue(e){
  let value =e.currentTarget.dataset.value
  this.setData({
    value
  })
  this.setList(value)
},
setList(value){
  let List = []
  let historyList = wx.getStorageSync('historyList')||[]
  let index = historyList.findIndex(v=>{return v===value})
  if(index<0){
    historyList.unshift(value)
  }
  wx.setStorageSync('historyList', historyList)
  const AllData = wx.getStorageSync('AllData')
  var pat=new RegExp(value);
  AllData.forEach(v=>{
    if(pat.test(v.name)){
      List.push(v)
    }
  })
  if(List.length===0){
    wx.showToast({
      title: '很抱歉，未搜索到该商品',
      icon:"none"
    })
  }
  this.setData({
    List,
    historyList,
    value:""
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 
  },

})